package com.dianping.cat.home.heavy.transform;

import com.dianping.cat.home.heavy.IVisitor;
import com.dianping.cat.home.heavy.entity.HeavyCache;
import com.dianping.cat.home.heavy.entity.HeavyCall;
import com.dianping.cat.home.heavy.entity.HeavyReport;
import com.dianping.cat.home.heavy.entity.HeavySql;
import com.dianping.cat.home.heavy.entity.Service;
import com.dianping.cat.home.heavy.entity.Url;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

/*  23 */   public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
/*  27 */     this.m_out = new DataOutputStream(out);
/*  28 */     this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(HeavyReport heavyReport) {
    ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

/*  34 */     build(heavyReport, out);
/*  35 */     return out.toByteArray();
  }


/*  39 */   public static void build(HeavyReport heavyReport, OutputStream out) { heavyReport.accept(new DefaultNativeBuilder(out)); }



  public void visitHeavyCache(HeavyCache heavyCache) {
/*  44 */     if (!heavyCache.getUrls().isEmpty()) {
/*  45 */       writeTag(33, 2);
/*  46 */       writeInt(heavyCache.getUrls().size());

/*  48 */       for (Url url : heavyCache.getUrls().values()) {
/*  49 */         url.accept(this.m_visitor);
      }
    }

/*  53 */     if (!heavyCache.getServices().isEmpty()) {
/*  54 */       writeTag(34, 2);
/*  55 */       writeInt(heavyCache.getServices().size());

/*  57 */       for (Service service : heavyCache.getServices().values()) {
/*  58 */         service.accept(this.m_visitor);
      }
    }

/*  62 */     writeTag(63, 3);
  }


  public void visitHeavyCall(HeavyCall heavyCall) {
/*  67 */     if (!heavyCall.getUrls().isEmpty()) {
/*  68 */       writeTag(33, 2);
/*  69 */       writeInt(heavyCall.getUrls().size());

/*  71 */       for (Url url : heavyCall.getUrls().values()) {
/*  72 */         url.accept(this.m_visitor);
      }
    }

/*  76 */     if (!heavyCall.getServices().isEmpty()) {
/*  77 */       writeTag(34, 2);
      writeInt(heavyCall.getServices().size());

/*  80 */       for (Service service : heavyCall.getServices().values()) {
/*  81 */         service.accept(this.m_visitor);
      }
    }

/*  85 */     writeTag(63, 3);
  }


  public void visitHeavyReport(HeavyReport heavyReport) {
/*  90 */     writeTag(63, 0);

/*  92 */     if (heavyReport.getDomain() != null) {
/*  93 */       writeTag(1, 1);
/*  94 */       writeString(heavyReport.getDomain());
    }

/*  97 */     if (heavyReport.getStartTime() != null) {
/*  98 */       writeTag(2, 1);
/*  99 */       writeDate(heavyReport.getStartTime());
    }

/* 102 */     if (heavyReport.getEndTime() != null) {
/* 103 */       writeTag(3, 1);
/* 104 */       writeDate(heavyReport.getEndTime());
    }

/* 107 */     if (heavyReport.getHeavySql() != null) {
/* 108 */       writeTag(33, 1);
/* 109 */       heavyReport.getHeavySql().accept(this.m_visitor);
    }

/* 112 */     if (heavyReport.getHeavyCall() != null) {
/* 113 */       writeTag(34, 1);
/* 114 */       heavyReport.getHeavyCall().accept(this.m_visitor);
    }

/* 117 */     if (heavyReport.getHeavyCache() != null) {
/* 118 */       writeTag(35, 1);
/* 119 */       heavyReport.getHeavyCache().accept(this.m_visitor);
    }

/* 122 */     writeTag(63, 3);
  }


  public void visitHeavySql(HeavySql heavySql) {
/* 127 */     if (!heavySql.getUrls().isEmpty()) {
/* 128 */       writeTag(33, 2);
/* 129 */       writeInt(heavySql.getUrls().size());

/* 131 */       for (Url url : heavySql.getUrls().values()) {
/* 132 */         url.accept(this.m_visitor);
      }
    }

/* 136 */     if (!heavySql.getServices().isEmpty()) {
/* 137 */       writeTag(34, 2);
/* 138 */       writeInt(heavySql.getServices().size());

/* 140 */       for (Service service : heavySql.getServices().values()) {
/* 141 */         service.accept(this.m_visitor);
      }
    }

/* 145 */     writeTag(63, 3);
  }


  public void visitService(Service service) {
/* 150 */     if (service.getDomain() != null) {
/* 151 */       writeTag(1, 1);
/* 152 */       writeString(service.getDomain());
    }

/* 155 */     if (service.getName() != null) {
/* 156 */       writeTag(2, 1);
/* 157 */       writeString(service.getName());
    }

/* 160 */     if (service.getLogview() != null) {
/* 161 */       writeTag(3, 1);
/* 162 */       writeString(service.getLogview());
    }

/* 165 */     writeTag(4, 0);
/* 166 */     writeLong(service.getCount());

/* 168 */     if (service.getKey() != null) {
/* 169 */       writeTag(5, 1);
/* 170 */       writeString(service.getKey());
    }

/* 173 */     writeTag(63, 3);
  }


  public void visitUrl(Url url) {
/* 178 */     if (url.getDomain() != null) {
/* 179 */       writeTag(1, 1);
/* 180 */       writeString(url.getDomain());
    }

/* 183 */     if (url.getName() != null) {
/* 184 */       writeTag(2, 1);
/* 185 */       writeString(url.getName());
    }

/* 188 */     if (url.getLogview() != null) {
/* 189 */       writeTag(3, 1);
/* 190 */       writeString(url.getLogview());
    }

/* 193 */     writeTag(4, 0);
/* 194 */     writeLong(url.getCount());

/* 196 */     if (url.getKey() != null) {
/* 197 */       writeTag(5, 1);
/* 198 */       writeString(url.getKey());
    }

/* 201 */     writeTag(63, 3);
  }

  private void writeDate(Date value) {
    try {
/* 206 */       writeVarint(value.getTime());
/* 207 */     } catch (Exception e) {
/* 208 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 214 */       writeVarint(value & 0xFFFFFFFFL);
/* 215 */     } catch (Exception e) {
/* 216 */       throw new RuntimeException(e);
    }
  }

  private void writeLong(long value) {
    try {
/* 222 */       writeVarint(value);
/* 223 */     } catch (Exception e) {
/* 224 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 230 */       this.m_out.writeUTF(value);
/* 231 */     } catch (IOException e) {
/* 232 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 238 */       this.m_out.writeByte((field << 2) + type);
/* 239 */     } catch (IOException e) {
/* 240 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 246 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 247 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 250 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 251 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/heavy/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */